﻿//-----------------------------------------------------------------------
// <copyright file="DisplayPrivateMessagesDataCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Windows.PrivateMessages
{
    using System.Globalization;
    using System.Windows;

    using ChatBox.UI.Commands;
    using ChatBox.Utility;

    /// <summary>
    /// Command to display new data on the private messages view model
    /// </summary>
    public class DisplayPrivateMessagesDataCommand : GenericCommandBase<PrivateMessagesViewModel>
    {
        /// <summary>
        /// Execute the command
        /// </summary>
        /// <param name="value">the view model</param>
        public override void ExecuteOnValue(PrivateMessagesViewModel value)
        {
            value.ScrollToBottom.MarkAtBottom();

            value.Filters.SetSelectionListsCommand.Execute(value.Filters);
            value.ApplyFilter();

            value.BulkDeleteEnabled = value.MessageListIsFiltered();

            value.Filters.FilteredListIsTruncatedToLatest = value.FilteredMessageList.Count > value.Filters.MessageCount;

            if (value.Filters.FilteredListIsTruncatedToLatest)
            {
                // show the latest few messages
                value.FilteredMessageList = value.FilteredMessageList.TakeLast(value.Filters.MessageCount);
            }

            value.MessagesDocument = value.MessageFormatter.GenerateMessagesDocument(
               value.FilteredMessageList,
               value.PrivateMessageFormatOptions);

            // if it was scrolled to the bottom, put it back there
            value.ScrollToBottom.ResetAtBottom();

            value.LoadingTextVisibility = Visibility.Collapsed;

            value.StatusText = GenerateStatusMessage(value);

            value.Filters.FilterEnabled = true;
            value.CanFilter = true;
            value.HasFilter = value.Filters.CurrentFilter != null;
        }

        /// <summary>
        /// Generate a string for the status message
        /// </summary>
        /// <param name="viewModel">the view model</param>
        /// <returns>the status message</returns>
        private static string GenerateStatusMessage(PrivateMessagesViewModel viewModel)
        {
            string formatString = "Showing {0} of {1} private messages";

            if (viewModel.ChatEngine.PrivateChatMessages.Count == viewModel.FilteredMessageList.Count)
            {
                formatString = "Showing all {0} private messages";
            }

            if (viewModel.Filters.FilteredListIsTruncatedToLatest)
            {
                if (viewModel.MessageListIsFiltered())
                {
                    formatString = "Showing the latest {0} of {1} filtered private messages";                    
                }
                else
                {
                    formatString = "Showing the latest {0} of {1} private messages";
                }
            }
            else if (viewModel.MessageListIsFiltered())
            {
                formatString = "Showing {0} filtered of {1} private messages";
            }

            return string.Format(
                CultureInfo.InvariantCulture,
                formatString,
                viewModel.FilteredMessageList.Count,
                viewModel.ChatEngine.PrivateChatMessages.Count);
        }
    }
}
